<?php

class Core_Table_Subject_Content extends Kd_Data_Table {

    private $_articleRelationTable = '{t}subject_article';
    private $_poiRelationTable = '{t}subject_poi';
    private $_roadRelationTable = '{t}subject_road';
    private $_industryRelationTable = '{t}subject_industry';
    private $_concessionsRelationTable = '{t}subject_concessions';

    public function __construct() {
        parent::__construct();
    }

    public function insertPoiRelation($subjectId, array $poiIds, $recommendIds = array(), $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_poiRelationTable)
                        ->value('subject_id', $subjectId)
                        ->value('poi_id', ':poiId:s')
                        ->value('is_recommend', ':recommend:s')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($poiIds as $poiId) {
            $values = array('poiId' => $poiId, 'recommend'=>0);
            if(is_array($recommendIds)){
                foreach($recommendIds as $reid){
                    if(strcmp($reid, $poiId) == 0){
                        $values['recommend'] = 1;
                        break;
                    }
                }
            }
            $driver->setSql($insert, $values)->execute();
        }
    }

    public function deletePoiRelation($subjectId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_poiRelationTable)
                ->where()->eq('subject_id', $subjectId)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function insertRoadRelation($subjectId, array $roadIds, $assignerId, $startTime = NULL, $endTime = NULL, $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_roadRelationTable)
                        ->value('subject_id', $subjectId)
                        ->value('assigner_id', $assignerId)
                        ->value('is_assigned', 1)
                        ->value('assigned_time', ':assignedTime:s')
                        ->value('assign_start_time', ':assignStartTime:s')
                        ->value('assign_end_time', ':assignEndTime:s')
                        ->value('road_id', ':roadId:s')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($roadIds as $roadId) {
            $driver->setSql($insert,
                    array('roadId' => $roadId,
                          'assignedTime'=>date(),
                          'assignStartTime'=>$startTime,
                          'assignEndTime'=>$endTime))->execute();
        }
    }

    public function deleteRoadRelation($subjectId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_roadRelationTable)
                ->where()->eq('subject_id', $subjectId)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function insertArticleRelation($subjectId, array $articleIds, $recommendIds = array(), $topIds = array(), $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_articleRelationTable)
                        ->value('subject_id', $subjectId)
                        ->value('article_id', ':articleId:i')
                        ->value('is_recommend', ':recommend:i')
                        ->value('is_top', ':top:i')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($articleIds as $articleId) {
            $values = array('articleId' => $articleId, 'recommend'=>0, 'top'=>0);
            if(is_array($recommendIds) && in_array($articleId, $recommendIds)){
                $values['recommend'] = 1;
            }
            if(is_array($topIds) && in_array($articleId, $topIds)){
                $values['top'] = 1;
            }
            $driver->setSql($insert, $values)->execute();
        }
    }

    public function deleteArticleRelation($subjectId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_articleRelationTable)
                ->where()->eq('subject_id', $subjectId)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function insertIndustryRelation($subjectId, array $indistryIds, $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_industryRelationTable)
                        ->value('subject_id', $subjectId)
                        ->value('industry_id', ':industryId:s')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($indistryIds as $industryId) {
            $driver->setSql($insert, array('industryId' => $industryId))->execute();
        }
    }

    public function deleteIndustryRelation($subjectId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_industryRelationTable)
                ->where()->eq('subject_id', $subjectId)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function insertConcessionsRelation($subjectId, array $concessionsIds, $recommendIds = array(),  $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_concessionsRelationTable)
                        ->value('subject_id', $subjectId)
                        ->value('concessions_id', ':concessionsId:i')
                        ->value('is_recommend', ':recommend:i')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($concessionsIds as $concessionsId) {
            $values = array('concessionsId' => $concessionsId, 'recommend'=>0);
            if(is_array($recommendIds) && in_array($concessionsId, $recommendIds)){
                $values['recommend'] = 1;
            }
            $driver->setSql($insert, $values)->execute();
        }
    }

    public function deleteConcessionsRelation($subjectId, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_concessionsRelationTable)
                ->where()->eq('subject_id', $subjectId)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

}